<?php

use yii\helpers\Html;
use kartik\grid\GridView;
use kartik\select2\Select2;
use yii\helpers\ArrayHelper;
use common\models\business\BusinessAdress;
use yii\bootstrap\Modal;
use yii\web\JsExpression;
/* @var $this yii\web\View */
/* @var $searchModel backend\modules\business\models\BusinessFarmersSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = '农户管理';
$this->params['breadcrumbs'][] = $this->title;
?>


<section class="wrapper site-min-height">
    <!-- page start-->
    <section class="panel">
        <header class="panel-heading">
              <?= Html::a('返回','javascript:history.back(-1)'); ?>  <?= Html::encode($this->title) ?>
        </header>
        <div class="panel-body">
            <div class="adv-table editable-table ">
 


<div class="business-farmers-index">

    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>

    <p>
        <?= Html::a('添加农户', ['create'], ['class' => 'btn btn-success']) ?>
    </p>


    <?php
        echo GridView::widget([
            'dataProvider'=>$dataProvider,
            'filterModel'=>$searchModel,
            'showPageSummary'=>true,
            'pjax'=>true,
            'striped'=>true,
            'hover'=>true,
            'panel'=>['type'=>'primary', 'heading'=>'农户管理'],
            'containerOptions'=>['style'=>'overflow: auto'], // only set when $responsive = false
            'headerRowOptions'=>['class'=>'kartik-sheet-style'],
            'filterRowOptions'=>['class'=>'kartik-sheet-style'],
            'columns'=>[
                [
                    'class'=>'kartik\grid\ExpandRowColumn',
                    'width'=>'50px',
                    'value'=>function ($model, $key, $index, $column) {
                        return GridView::ROW_COLLAPSED;
                    },
                    'detail'=>function ($model, $key, $index, $column) {
                        return Yii::$app->controller->renderPartial('_pigs', ['model'=>$model]);
                    },
                    'headerOptions'=>['class'=>'kartik-sheet-style'],
                    'expandOneOnly'=>true
                ],
                [
                    'attribute'=>'id',
                    'pageSummaryOptions'=>['class'=>'text-right text-warning'],
                    'width'=>'100px',
                ],
                [
                    'attribute'=>'gender',
                    'pageSummaryOptions'=>['class'=>'text-right text-warning'],
                    'width'=>'50px',
                ],
                [
                    'attribute'=>'name',
                    'pageSummaryOptions'=>['class'=>'text-right text-warning'],
                    'width'=>'100px',
                ],

                [
                    'attribute' => 'countyid',
                    'value'=>'county.name',
                    'pageSummaryOptions'=>['class'=>'text-right text-warning'],
                    'width'=>'100px',
                    'filterType'=>GridView::FILTER_SELECT2,
                    'filter'=>ArrayHelper::map(BusinessAdress::find()->where(['type'=>BusinessAdress::TYPE_XIAN])->asArray()->all(),'id','name'),
                    'filterWidgetOptions'=>[
                        'pluginOptions'=>['allowClear'=>true],
                    ],
                    'filterInputOptions'=>['placeholder'=>'县'],
                ],
                [
                    'attribute' => 'townid',
                    'value'=>'town.name',
                    'pageSummaryOptions'=>['class'=>'text-right text-warning'],
                    'width'=>'100px',
                    'filterType'=>GridView::FILTER_SELECT2,
                    'filter'=>ArrayHelper::map(BusinessAdress::find()->where(['type'=>BusinessAdress::TYPE_ZHEN])->asArray()->all(),'id','name'),
                    'filterWidgetOptions'=>[
                        'pluginOptions'=>['allowClear'=>true],
                    ],
                    'filterInputOptions'=>['placeholder'=>'镇'],
                ],
                [
                    'attribute' => 'villageid',
                    'value'=>'village.name',
                    'pageSummaryOptions'=>['class'=>'text-right text-warning'],
                    'width'=>'100px',
                    'filterType'=>GridView::FILTER_SELECT2,
                    'filter'=>ArrayHelper::map(BusinessAdress::find()->where(['type'=>BusinessAdress::TYPE_CUN])->asArray()->all(),'id','name'),
                    'filterWidgetOptions'=>[
                        'pluginOptions'=>['allowClear'=>true],
                    ],
                    'filterInputOptions'=>['placeholder'=>'村'],
                ],

                [
                    'attribute'=>'address',
                    'pageSummaryOptions'=>['class'=>'text-right text-warning'],
                    'width'=>'100px',
                ],
                [
                    'attribute'=>'phone',
                    'pageSummaryOptions'=>['class'=>'text-right text-warning'],
                    'width'=>'100px',
                ],

                [
                    'attribute' => 'introducer',
                    'format' => 'raw',
                    'value' => function($model){
                        return $model->person?Html::a($model->person->name,\yii\helpers\Url::to(['/business/person/view','id'=> $model->person->id])):'';
                    },
                    'pageSummaryOptions'=>['class'=>'text-right text-warning'],
                    'width'=>'100px',
                    'filterType'=>GridView::FILTER_SELECT2,
                    'filter'=>ArrayHelper::map(\common\models\business\BusinessPerson::find()->asArray()->all(),'id','name'),
                    'filterWidgetOptions'=>[
                        'pluginOptions'=>['allowClear'=>true],
                    ],
                    'filterInputOptions'=>['placeholder'=>'业务员'],
                ],
                [
                    'attribute'=>'identity',
                    'pageSummaryOptions'=>['class'=>'text-right text-warning'],
                    'width'=>'100px',
                ],
                [
                    'attribute'=>'status',
                    'value'=>function($model){
                        return \common\models\business\BusinessFarmers::$statues[$model->status];
                    },
                    'pageSummaryOptions'=>['class'=>'text-right text-warning'],
                    'width'=>'100px',
                    'filterType'=>GridView::FILTER_SELECT2,
                    'filter'=>\common\models\business\BusinessFarmers::$statues,
                    'filterWidgetOptions'=>[
                        'pluginOptions'=>['allowClear'=>true],
                    ],
                    'filterInputOptions'=>['placeholder'=>'状态'],
                ],
                [
                    'attribute' => 'ispoor',
                    'value'=>function($model){
                        return \common\models\business\BusinessFarmers::$ispoor[$model->ispoor];
                    },
                    'pageSummaryOptions'=>['class'=>'text-right text-warning'],
                    'width'=>'100px',
                    'filterType'=>GridView::FILTER_SELECT2,
                    'filter'=>\common\models\business\BusinessFarmers::$ispoor,
                    'filterWidgetOptions'=>[
                        'pluginOptions'=>['allowClear'=>true],
                    ],
                    'filterInputOptions'=>['placeholder'=>'是否贫困'],
                ],
                [
                    'class' => '\kartik\grid\ActionColumn',
                    'deleteOptions' => ['label' => '<i class="glyphicon glyphicon-remove"></i>']
                ],
                [
                    'class' => '\kartik\grid\CheckboxColumn'
                ]
            ],
            'toolbar'=> [
                [
                    'content' => Html::button('<i class="glyphicon glyphicon-delete">批量设置农户巡检员</i>', ['type'=>'button', 'title'=>'批量农户巡检员', 'class'=>'btn btn-success', 'id'=>'setperson']),
                ],
                [
                    'content' => Html::button('<i class="glyphicon glyphicon-delete">审核通过</i>', ['type'=>'button', 'title'=>'批量审核通过', 'class'=>'btn btn-primary', 'id'=>'setpass']),
                ],
                '{export}',
                '{toggleData}',
            ],
            'bordered'=>true,
            'condensed'=>true,
        ]);
        ?>


    <!--设置出栏时间-->
    <?php
        Modal::begin([
            'options' => [
                'id' => 'setperson-modal',
                'tabindex' => false // important for Select2 to work properly
            ],
            'header' => '<h4 style="margin:0; padding:0">批量设置农户巡检员</h4>',
        ]);

        $form = \yii\widgets\ActiveForm::begin([
            'action' => '/business/farmers/set-person',
            'options' => ['class' => 'form-horizontal'],
            'fieldConfig' => [
                'template' => "<div class='col-xs-3 col-sm-2 text-right'>{label}</div><div class='col-xs-9 col-sm-7'>{input}</div><div class='col-xs-12 col-xs-offset-3 col-sm-3 col-sm-offset-0'>{error}</div>",
            ]
        ]);

        echo Select2::widget([
            'name' => 'introducer',
            'options' => ['placeholder' => '输入巡检员的姓名 ...'],
            'initValueText'=>'',
            'pluginOptions' => [
                'allowClear' => true,
                'minimumInputLength' => 1,//重要
                'language' => [
                    'errorLoading' => new JsExpression("function () { return 'Waiting...'; }"),
                ],
                'ajax' => [
                    'url' => '/business/search/search-person',
                    'dataType' => 'json',
                    'data' => new JsExpression('function(params) { return {q:params.term}; }'),
                    'cache' => true
                ],
                'width' => '400px',
                'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
                'templateResult' => new JsExpression('function(res) { return res.text; }'),
                'templateSelection' => new JsExpression('function (res) { return res.text; }'),
            ],
        ]);

        echo Html::input('hidden','ids','',['id'=>'ids']);
        echo Html::submitButton('确认更新', ['class' => 'btn btn-success','style'=>'margin-top:20px;']);
        \yii\widgets\ActiveForm::end();
        Modal::end();
    ?>



</div>
</div>
</div>
</section>
</section>

<script>
    <?php $this->beginBlock('js_end') ?>


    $(function () {
        //批量设置巡检人员
        $('#setperson').click(function () {
            var keys = jqchk();
            if (keys.length == 0) {
                alert('请选择要设置的农户');
                return false;
            }
            $('#setperson-modal').modal();
            $('#ids').val(keys.join(','));

        });

        $('#setpass').click(function () {
            var keys = jqchk();
            if(confirm('确定要审核通过选中的农户')){
                if(keys.length <= 0){
                    alert('请选中对应的农户');
                    return false;
                }
                $.post('/business/farmers/setpass',{'ids':keys},function(res){
                    console.log(res);
                    alert(res.msg);
                    if(res.status == 0)
                        window.location.href='/business/farmers/index';
                },'json');
            }
        });


        function jqchk() { //jquery获取复选框值=
            var chk_value = [];
            $('input[name="selection[]"]:checked').each(function () {
                chk_value.push($(this).val());
            });
            return chk_value;
        }
    })

    <?php $this->endBlock(); ?>
    <?php $this->registerJs($this->blocks['js_end'],\yii\web\View::POS_END);//将编写的js代码注册到页面底部 ?>
</script>




